package com.example.Base;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.ActionBar;
import android.graphics.drawable.Drawable;

import com.example.administrator.olddoctor.R;
import com.example.tools.MyProgressBar;

import net.tsz.afinal.FinalHttp;

/**
 * Created by Administrator on 2016/9/26.
 */
public class BaseActivity extends FragmentActivity{
    protected MyProgressBar mPbar;
    protected FinalHttp mFinalHttp;
    private static Toast mToast = null;

    private ImageView actionBarLeft;
    private TextView actionBarLeftText;
    private ImageView actionBarCenterPic;
    private TextView actionBarTitle;
    private ImageView actionBarRight;
    private TextView actionBarRightText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // requestQueue = Volley.newRequestQueue(BaseActivity.this);
        // mContext = BaseActivity.this;
        mPbar = new MyProgressBar(this);
        mFinalHttp = new FinalHttp();
        mFinalHttp.configTimeout(30000);


        if (getActionBar() != null) {
            getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            getActionBar().setCustomView(R.layout.actionbar);
            getActionBar().setDisplayShowCustomEnabled(true);
            initActionBar();
        }

    }

    private void initActionBar() {
        actionBarLeft = (ImageView) findViewById(R.id.actionbar_left);
        actionBarLeftText = (TextView) findViewById(R.id.actionbar_left_text);
        actionBarCenterPic = (ImageView) findViewById(R.id.actionbar_center_img);
        actionBarTitle = (TextView) findViewById(R.id.actionbar_title);
        actionBarRight = (ImageView) findViewById(R.id.actionbar_right);
        actionBarRightText = (TextView) findViewById(R.id.actionbar_right_text);

        actionBarTitle.setText(getResources().getString(R.string.app_name));
    }

    public void setActionBar(Drawable leftPicId, String leftText, String title,
                             Drawable centerPic, Drawable rightPicId, String rightText) {
        if (getActionBar() != null) {
            actionBarLeft.setImageDrawable(leftPicId);
            actionBarLeftText.setText(leftText);
            actionBarCenterPic.setImageDrawable(centerPic);
            actionBarTitle.setText(title);
            actionBarRight.setImageDrawable(rightPicId);
            actionBarRightText.setText(rightText);
        }
    }

    /**
     * 显示Toast提示
     */
    protected void showToast(String text) {
        if (mToast == null) {
            mToast = Toast.makeText(BaseActivity.this, text,Toast.LENGTH_SHORT);
        } else {
            mToast.setText(text);
            mToast.setDuration(Toast.LENGTH_SHORT);
        }
        mToast.show();
    }

    public ImageView getActionBarLeft() {
        return actionBarLeft;
    }

    public void setActionBarLeft(ImageView actionBarLeft) {
        this.actionBarLeft = actionBarLeft;
    }

    public TextView getActionBarLeftText() {
        return actionBarLeftText;
    }

    public void setActionBarLeftText(TextView actionBarLeftText) {
        this.actionBarLeftText = actionBarLeftText;
    }

    public ImageView getActionBarCenterPic() {
        return actionBarCenterPic;
    }

    public void setActionBarCenterPic(ImageView actionBarCenterPic) {
        this.actionBarCenterPic = actionBarCenterPic;
    }

    public TextView getActionBarTitle() {
        return actionBarTitle;
    }

    public void setActionBarTitle(TextView actionBarTitle) {
        this.actionBarTitle = actionBarTitle;
    }

    public ImageView getActionBarRight() {
        return actionBarRight;
    }

    public void setActionBarRight(ImageView actionBarRight) {
        this.actionBarRight = actionBarRight;
    }

    public TextView getActionBarRightText() {
        return actionBarRightText;
    }

    public void setActionBarRightText(TextView actionBarRightText) {
        this.actionBarRightText = actionBarRightText;
    }

    public String getVersionCode() {
        PackageManager packageManager = getPackageManager();
        PackageInfo packageInfo = null;
        try {
            packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return packageInfo.versionCode + "";
    }

    public String getVersionName() {
        PackageManager packageManager = getPackageManager();
        PackageInfo packageInfo = null;
        try {
            packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return packageInfo.versionName + "";
    }

    public void dismiss() {
        mPbar.dismiss();
    }
}
